﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// 相机适配
    /// </summary>
    [RequireComponent(typeof(Camera))]
    public class OrthographicCameraAdapter : MonoBehaviour
    {
        public Vector2Int resolution;

        private Camera curCamera;
        private float defaultSize;

        private void Awake()
        {
            curCamera = GetComponent<Camera>();
            defaultSize = curCamera.orthographicSize;
            FitCamera();
        }

        /// <summary>
        /// 相机适配
        /// </summary>
        public void FitCamera()
        {
            if (!curCamera.orthographic)
            {
                Debug.LogError("这个脚本只用于orthographic相机的竖屏适配");
                return;
            }
            float oldScale = (float)resolution.y / resolution.x;
            float newScale = (float)Screen.height / Screen.width;
            if (newScale > oldScale)
            {
                curCamera.orthographicSize = newScale * defaultSize / oldScale;
            }
        }
    }
}